from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
from lxml import etree

class BingInfo:
    def __init__(self):
        self.keywork='xpath'#可更改
        self.url='https://cn.bing.com'


    def search(self):
        self.driver = webdriver.Chrome("chromedriver.exe")
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        keywork=self.driver.find_element_by_xpath('//input[@id="sb_form_q"]')
        keywork.send_keys(self.keywork)#输入查找的关键字
        # 方法一：
        # keywork.send_keys(Keys.ENTER)#回车按钮
        # 方法二:
        time.sleep(1)
        # 使用点击搜索按钮进行查询
        self.driver.find_element_by_xpath("//label[@for='sb_form_go']").click()
        # 点击
        time.sleep(1)
        html=self.driver.page_source
        return html

    def page_sourch(self,html):
        #解析页面信息
        mql_html=etree.HTML(html)
        #获取标题以及其链接
        html_link=mql_html.xpath("//li[@class='b_algo']/div/h2/a/@href")
        #获取标题
        text_html=mql_html.xpath("//li[@class='b_algo']/div/h2/a/text()")
        for i,h in zip(html_link,text_html):
            print(h,i)


if __name__=='__main__':
    mql=BingInfo()
    html=mql.search()
    mql.page_sourch(html)






